
package com.bear.net.httpclient;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.google.gson.Gson;

/**
 * 解析工具类
 * 
 * @author yinghui.hong
 */
public class ParseUtil {
    private static final String TAG = "ParseUtil";

    private static String REQUEST = "req";

    private static String RESPONSE = "res";

    /**
     * 转HashMap对象为字符串。 默认requestKey为"req"。
     * 
     * @param valueMap
     * @return e.g.{"req":[{"reqtype":"mobile_query","phone":"13599050189"}]}
     */
    public static String parseMap2Str(HashMap<String, String> valueMap) {
        return parseMap2Str(valueMap, REQUEST);
    }

    /**
     * 转HashMap对象为字符串。
     * 
     * @param valueMap
     * @param requestKey e.g. "req"
     * @return e.g.{"req":[{"reqtype":"mobile_query","phone":"13599050189"}]}
     */
    public static String parseMap2Str(HashMap<String, String> valueMap, String requestKey) {
        JSONObject jsonParent = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonChild = new JSONObject();
        try {
            for (String key : valueMap.keySet()) {
                jsonChild.put(key, valueMap.get(key));
            }
            jsonArray.put(jsonChild);
            jsonParent.put(requestKey, jsonArray);
            Log.i(TAG, "REQUEST STRING IS --- " + jsonParent.toString());
            return jsonParent.toString();
        } catch (JSONException e) {
            Log.e(TAG, e.toString());
        }
        return null;
    }

    /**
     * 解析JSON字符串为对象。默认responseKey为"res"
     * 
     * @param json e.g.{"res":{"code":"1","msg":"请检查输入手机号是否正确"}}
     * @param classOfResponseBean ResponseBean的子类
     * @return ResponseBean的实例对象
     * @throws JSONException 
     */
    public static ResponseBean parseJson2Obj(String json,
            Class<? extends ResponseBean> classOfResponseBean) throws JSONException {
        return parseJson2Obj(json, classOfResponseBean, RESPONSE);
    }

    /**
     * 解析JSON字符串为对象。
     * 
     * @param json e.g.{"res":{"code":"1","msg":"请检查输入手机号是否正确"}}
     * @param classOfResponseBean ResponseBean的子类
     * @param responseKey e.g. "res"
     * @return ResponseBean的实例对象
     * @throws JSONException 
     */
    public static ResponseBean parseJson2Obj(String json,
            Class<? extends ResponseBean> classOfResponseBean, String responseKey) throws JSONException {
        ResponseBean bean = null;

            Log.i(TAG, "RESPONSE STRING IS --- " + json);
            JSONObject jsonParent = new JSONObject(json);
            JSONObject jsonChild = jsonParent.optJSONObject(responseKey);
            // FUNO FIX OK请使用变量
            Log.i(TAG, "jsonObjectChild" + jsonChild.toString());
            Gson gson = new Gson();
            bean = gson.fromJson(jsonChild.toString(), classOfResponseBean);

        return bean;
    }

}
